package org.whb.pet.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.whb.basic.service.impl.BaseServiceImpl;
import org.whb.pet.domain.Pet;
import org.whb.pet.domain.PetDetail;
import org.whb.pet.mapper.PetDetailMapper;
import org.whb.pet.mapper.PetMapper;
import org.whb.pet.service.IPetDetailService;
import org.whb.pet.service.IPetService;
import org.whb.user.domain.Logininfo;
import org.whb.user.domain.User;
import org.whb.user.mapper.UserMapper;
import org.whb.user.service.IUserService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void onSale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("onSale", new Date());
        petMapper.onSale(map);
    }

    @Override
    public void offSale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("offSale", new Date());
        petMapper.offSale(map);
    }

    @Override
    public Pet queryByDetail(Long id) {
        Pet pet = petMapper.loadByIdDetail(id);
        System.out.println(pet);
        return pet;
    }

    @Override
    public void adopt(Long id, Logininfo logininfo) {
        Pet pet = petMapper.loadById(id);
        pet.setState(0);
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        pet.setUser_id(user.getId());
        pet.setOffsaletime(new Date());
        petMapper.update(pet);
    }

    @Override
    public void add(Pet pet) {
        //返回自增主键
        //默认是下架状态
        pet.setState(0);
        petMapper.save(pet);
        //获取宠物详情
        PetDetail detail = pet.getDetail();
        if(detail!=null){
            Long id = pet.getId();
            detail.setPet_id(id);
            petDetailMapper.save(detail);
        }
    }
}
